package cn.anquing.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;


public class ImageMarkLogoByIcon {
    
    /**   
     * @param args   
     */    
    public static void main(String[] args) {
    	String fromPath = "D:/testimg/";     
        String iconPath = "D:/testimg/logo.png";
        String toPath = "D:/testimg/";
        //给图片添加水印
        ImageMarkLogoByIcon.markImageByIcon(iconPath,fromPath,toPath,"c.jpg",30);
        System.out.println("执行完成！");
    }
    
    
 
    /**   
     * 给图片添加水印、可设置水印图片旋转角度   
     * @param iconPath 水印图片路径   
     * @param fromPath 源图片路径   
     * @param toPath 目标图片路径  
     * @param fileName 源图片名
     * @param degree 水印图片旋转角度 
     * 
     * @return 目标图片名：mark_fileName
     */    
    public static String markImageByIcon(String iconPath,String fromPath,String toPath,String srcName, Integer degree) {     
            OutputStream os = null;
            String markName = "mark_"+srcName;
        try {
        	//原图片
            Image srcImg = ImageIO.read(new File(fromPath+srcName));         
            int width = srcImg.getWidth(null);
            int height = srcImg.getHeight(null);
            
            BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            
            // 得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            //将图片画到画笔对象中
            g.drawImage(srcImg, 0,0, width, height,null);
            
            
            //水印图象 水印一般为gif或者png的这样可设置透明度 
            Image imgIcon = ImageIO.read(new File(iconPath));
            int width1 = imgIcon.getWidth(null);
            int height1 = imgIcon.getHeight(null);
            float alpha = 0.5f; // 透明度 
            
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));            
            
            // 设置水印旋转     
            g.rotate(Math.toRadians(degree),buffImg.getWidth()/2,buffImg.getHeight()/2);
           
            
            int x = -width/2;
            int y = -height/2;
            
            while(x<width*1.5){
            	
            	y = -height/2;
            	
            	while(y<height*1.5){
            		g.drawImage(imgIcon, x, y, null);
            		
            		y += height1;
            	}
            	x += width1;
            } 
            g.dispose();           
            os = new FileOutputStream(toPath+markName);
            // 生成图片     
            ImageIO.write(buffImg, "JPG", os);
            DeleteFileUtil.deleteFile(fromPath,srcName);
            return markName;
        } catch (Exception e) {     
            e.printStackTrace();
            return null;
        } finally {     
            try {     
                if (null != os)     
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();     
            }     
        }     
    }
    
    
}   